package com.sxz.sbs.module.client;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.AddUserListAdapter;
import com.sxz.sbs.module.client.bean.AddUserListBean;
import com.sxz.sbs.module.client.presenter.AddUserListPresenter;
import com.sxz.sbs.module.client.view.AddUserListView;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 *
 * @author hp
 * @date 2018/3/21
 * 推荐用户列表
 */
@Route(path = RouterUrl.SHOW_RELATTION)
public class RecommendUserActivity extends BaseActivity<AddUserListPresenter>  implements AddUserListView {

    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_nav_title)
    TextView tv_nav_title;
    private AddUserListAdapter addUserListAdapter;
    private int currentPage = 1;
    private int totalRow=0;
    private List<AddUserListBean.ListBean> dataList = new ArrayList<>();

    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("推荐用户").build().show();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getAddUserData();
            }
        });
        refreshLayout.setRefreshing(true);
        getAddUserData();
        addUserListAdapter = new AddUserListAdapter(dataList);
        recyclerView.setAdapter(addUserListAdapter);
        addUserListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage++;
                getAddUserData();
            }
        }, recyclerView);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recommenduser;
    }

    @Override
    protected AddUserListPresenter createPresenter() {
        return new AddUserListPresenter();
    }

    private void getAddUserData(){
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("pageNumber",currentPage);
        mPresenter.getAddUserList(map);
    }


    @Override
    public void onGetUserListSuccess(AddUserListBean bean) {
        if (mIsLive && bean != null) {
            refreshLayout.setRefreshing(false);
            tv_nav_title.setText("推荐用户("+bean.getTotalRow()+")");
            if (bean.getList() != null && bean.getList().size() > 0) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(bean.getList());
                    addUserListAdapter.notifyDataSetChanged();
                } else {
                    dataList.addAll(bean.getList());
                    addUserListAdapter.notifyDataSetChanged();
                }
                addUserListAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    addUserListAdapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    addUserListAdapter.loadMoreEnd();
                }
            }
        }
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (errorTag == AppConstant.NET_ERROR_CODE) {
            stateLayout.showNetwork();
        } else {
            stateLayout.showEmpty();
        }

    }
}
